﻿var Login = function () {

    var handleLogin = function () {
        $('.login-form').validate({
            errorElement: 'span', //default input error message container
            errorClass: 'help-block', // default input error message class
            focusInvalid: false, // do not focus the last invalid input
            rules: {
                username: {
                    required: true
                },
                password: {
                    required: true
                },
                remember: {
                    required: false
                }
            },

            messages: {
                username: {
                    required: "请输入用户名"
                },
                password: {
                    required: "请输入密码"
                }
            },

            invalidHandler: function (event, validator) { //display error alert on form submit   
                $('.alert-danger > span').html("请输入账号和密码");
                $('.alert-danger', $('.login-form')).show();
            },

            highlight: function (element) { // hightlight error inputs
                $(element)
	                    .closest('.form-group').addClass('has-error'); // set error class to the control group
            },

            success: function (label) {
                label.closest('.form-group').removeClass('has-error');
                label.remove();
            },

            errorPlacement: function (error, element) {
                error.insertAfter(element.closest('.input-icon'));
            },

            submitHandler: function (form) {
                //form.submit(); // form validation success, call ajax form submit
                var btnSubmit = $("#btnSubmit");
                var strLoginDefault = '登陆';
                var strLogin = '<i class="fa fa-spinner fa-spin"></i> 登陆中';
                var postData = {};
                var alertBox = $('.alert-danger');

                btnSubmit.html(strLogin);
                btnSubmit.attr("disabled", "disabled");

                postData.username = $("#username").val();
                postData.password = $("#password").val();
                postData.remember = $("#remember").is(":checked");

                $.ajax({
                    url: BASE_PATH + 'C/Login' + window.location.href.replace(/[^\?]+/, ''),
                    type: 'POST',
                    data: postData,
                    timeout: 5000,
                    success: function (data) {
                        alertBox.hide();
                        if (data.Result) {
                            window.location.href = data.Data;
                        } else {
                            btnSubmit.html(strLoginDefault);
                            btnSubmit.removeAttr("disabled");
                            alertBox.children("span").html(data.Message);
                            alertBox.show();
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        btnSubmit.html(strLoginDefault);
                        btnSubmit.removeAttr("disabled");

                        switch (textStatus) {
                            case "timeout":
                                alertBox.children("span").html("服务器未响应");
                                alertBox.show();
                                break;
                        }
                    }
                });
            }
        });

        $('.login-form input').keypress(function (e) {
            if (e.which == 13) {
                if ($('.login-form').validate().form()) {
                    $('.login-form').submit();
                }
                return false;
            }
        });
    }

    var handleForgetPassword = function () {
        $('.forget-form').validate({
            errorElement: 'span', //default input error message container
            errorClass: 'help-block', // default input error message class
            focusInvalid: false, // do not focus the last invalid input
            ignore: "",
            rules: {
                email: {
                    required: true,
                    email: true
                }
            },

            messages: {
                email: {
                    required: "Email is required."
                }
            },

            invalidHandler: function (event, validator) { //display error alert on form submit   

            },

            highlight: function (element) { // hightlight error inputs
                $(element)
	                    .closest('.form-group').addClass('has-error'); // set error class to the control group
            },

            success: function (label) {
                label.closest('.form-group').removeClass('has-error');
                label.remove();
            },

            errorPlacement: function (error, element) {
                error.insertAfter(element.closest('.input-icon'));
            },

            submitHandler: function (form) {
                var alertMsg = $("#forget");

                alertMsg.removeClass();
                alertMsg.addClass("alert alert-warning");

                alertMsg.html("正在请求服务器，请稍后……");

                $.ajax({
                    url: "/C/ForgetPassword",
                    type: "POST",
                    data: { "mailaddress": $("#femail").val() },
                    timeout: 5000,
                    success: function (rsp) {
                        alertMsg.removeClass("alert-warning");

                        if (rsp.Result) {
                            alertMsg.addClass("alert-success");
                        } else {
                            alertMsg.addClass("alert-danger");
                        }

                        alertMsg.html(rsp.Message);
                    },
                    error: function (a, b, c) {
                        alertMsg.removeClass("alert-warning");

                        switch (b) {
                            case "timeout":
                                alertMsg.html("服务器未响应");
                                break;
                        }

                        alertMsg.addClass("alert-danger");
                    }
                })
            }
        });

        $('.forget-form input').keypress(function (e) {
            if (e.which == 13) {
                if ($('.forget-form').validate().form()) {
                    $('.forget-form').submit();
                }
                return false;
            }
        });

        jQuery('#forget-password').click(function () {
            jQuery('.login-form').hide();
            jQuery('.forget-form').show();
        });

        jQuery('#back-btn').click(function () {
            jQuery('.login-form').show();
            jQuery('.forget-form').hide();
        });

    }

    return {
        //main function to initiate the module
        init: function () {
            handleLogin();
            handleForgetPassword();
        }

    };

} ();
